<?php
class LoginForm extends CFormModel
{
	public $adminName;
	public $adminPass;
    public $checkCode;

	private $_identity;

	public function rules()
	{
		return array(
			array('adminName, adminPass, checkCode', 'required'),
			array('adminPass', 'authenticate'),
            array('checkCode', 'captcha'),
		);
	}

	public function attributeLabels()
	{
		return array(
            'adminName'=>'管理员名称',
            'adminPass'=>'管理员密码',
            'checkCode'=>'验证码'
		);
	}

	public function authenticate($attribute, $params)
	{
        if(!$this->hasErrors())
		{
			$this->_identity = new UserIdentity($this->adminName, $this->adminPass);
			if(!$this->_identity->authenticate())
				$this->addError('adminPass','管理员密码错误');
		}
	}

	public function login()
	{
		if($this->_identity===null)
		{
			$this->_identity=new UserIdentity($this->adminName, $this->adminPass);
			$this->_identity->authenticate();
		}
		if($this->_identity->errorCode===UserIdentity::ERROR_NONE)
		{
            Yii::app()->user->login($this->_identity);
			return true;
		}
		else {
            return false;
        }
	}
}
